exports.controllers={
	admin_forms: function(space) {
		if (!space.roles.forms) return space.views.process("errors/401",space);
		var mode=space.fields.mode||"list";
		return space.controllers['admin_forms__'+mode](space);
	},
	admin_forms__list: function(space) {
		space.forms=space.mfact.list("form",{order_by:"main.name"});
		return space.views.process("widgets/admin_forms/list",space);
	},
	admin_forms__sql: function(space) {
		space.form=space.mfact.get("form",space.fields.form_id);
		var config;
		eval("config="+space.form.config);
		var crt="";
		var fkeys="";
		crt+="create table T"+(space.form.code||space.form.name)+"s (\n";
		crt+="	id		serial primary key";
		for (var i=0;i<config.widgets.length;i++) {
			var w=config.widgets[i].options;
			if (w.type=="label") continue;
			crt+=",\n	"+w.name+"\t\t";
			switch (w.type) {
				case "checkbox":
					crt+="integer not null";
					break;
				case "date":
					crt+="date";
					break;
				case "fileupload":
					crt+="integer";
					break;
				case "select":
					if (w.name.match(/_id$/)) {
						crt+="integer";
						fkeys+=",\n	foreign key ("+w.name+") references ";
						if (typeof(w.values)=="string") {
							var arr=w.values.match(/ModelName:\s*"(\w+)"/);
							if (arr) fkeys+="T"+arr[1]+"s";
						}
					} else {
						crt+="varchar(256)";
					}
					break;
				case "textareaedit":
					crt+="text";
					break;
				case "textedit":
					crt+="varchar(2000)";
					break;
				default:
					throw "Unknown type "+w.type;
			}
		}
		crt+=fkeys;
		crt+="\n);\n";
		space.create_table=crt;
		return space.views.process("widgets/admin_forms/sql",space);
	},
	admin_forms__edit: function(space) {
		space.form_styles=space.mfact.list("form_style",{order_by: "main.name"});
		if (space.fields.form_id) {
			space.form=space.mfact.get("form",space.fields.form_id);
		} else {
			space.form={
				name: "",
				header: "",
				config: "{style_id: "+space.form_styles[0].id+",widgets: []}"
			};
		}
		space.form_styles.unshift({id:"",name:""});
		return space.views.process("widgets/admin_forms/edit",space);
	},
	admin_forms__save: function(space) {
		var item;
		if (space.fields.id) {
			item=space.mfact.get("form",space.fields.id);
			item.UpdateFromObject(space.fields);
		} else{
			item=space.mfact.from_object("form",space.fields);
		}
		item.Save();
		return JSON.stringify({redirect:{url: space.action}});
	}/*,
	admin_forms__ds_edit: function(space) {
		my ($tmpl,$space,$query)=@_;
		$space->{form_datasources}=[map {{rowClass=>"ds",id=>$_->{id},cells=>[$_->{datasource_id},$_->{prefix},$_->{params}]}} $tmpl->{sql}->execute_bind_and_fetch("form_datasources/list_of_form",{},$query)];
		$space->{datasources}=[map { {id=>$_->{id},name=>$_->{filename}} } $tmpl->{sql}->execute_bind_and_fetch("datasources/list",$space,$query)];
		return $tmpl->process("widgets/forms/ds_edit",$space,$query);
	},
	sub admin_forms__ds_save {
		my ($tmpl,$space,$query)=@_;
		my $not_used={};
		foreach my $i ($tmpl->{sql}->execute_bind_and_fetch("form_datasources/list_of_form",{},$query)) {
			$not_used->{$i->{id}}=1;
		}
		my $grid_data=$tmpl->parse_grid_submitted($space,$query,"dss-");
		while (my ($id,$href)=each %$grid_data) {
			if (substr($id,0,5) eq "newid") {
				$tmpl->{sql}->execute_bind("form_datasources/insert",$href,$query);
			} else {
				$href->{form_datasource_id}=$id;
				$tmpl->{sql}->execute_bind("form_datasources/update",$href,$query);
				delete $not_used->{$id};
			}
		}
		foreach my $id (keys %$not_used) {
			$tmpl->{sql}->execute_bind("form_datasources/delete",{form_datasource_id=>$id});
		}
		return $tmpl->JSON({redirect=>{url=>$space->{action}}});
	}*/,
	/*,
	sub admin_forms__w_save {
		my ($tmpl,$space,$query)=@_;
		my $widgets={};
		foreach my $param ($query->param()) {
			if ($param=~/^widget-(\w+)-(\w+)$/) {
				$widgets->{$1}->{$2}=$query->param($param);
			}
		}
		my $old={};
		foreach my $i ($tmpl->{sql}->execute_bind_and_fetch("form_widgets/list_for_editor",{},$query)) {
			$old->{$i->{id}}=1;
		}
		my $newids={};
		while (my ($wid,$href)=each %$widgets) {
			if ($wid=~/^new/) {
				$tmpl->{sql}->execute_bind("form_widgets/insert",$href,$query);
				$newids->{$wid}=$tmpl->{sql}->{dbh}->last_insert_id(undef,undef,"tform_widgets",undef);
			} else {
				$href->{form_widget_id}=$wid;
				$tmpl->{sql}->execute_bind("form_widgets/update",$href,$query);
				delete $old->{$wid};
			}
		}
		foreach my $wid (keys %$old) {
			$tmpl->{sql}->execute_bind("form_widgets/delete",{form_widget_id=>$wid});
		}
		my $ord=-1;
		my @ordarr1=();
		my @ordarr2=();
		foreach my $wid (split /,/,$query->param("ordering")) {
			$wid=~s/widget-//;
			$wid=$newids->{$wid} if $newids->{$wid};
			push(@ordarr1,$wid);
			push(@ordarr2,$tmpl->{sql}->execute_bind_and_fetch_one("form_widgets/get_ordering",{form_widget_id=>$wid})->{ordering});
			$tmpl->{sql}->execute_bind("form_widgets/set_ordering",{form_widget_id=>$wid,ordering=>$ord});
			$ord--;
		}
		@ordarr2=sort @ordarr2;
		my $j=0;
		foreach my $wid (@ordarr1) {
			$tmpl->{sql}->execute_bind("form_widgets/set_ordering",{form_widget_id=>$wid,ordering=>$ordarr2[$j]});
			$j++;
		}
		return $tmpl->JSON({redirect=>{url=>$space->{action}}});
	#	return $tmpl->render_http_error($space,$query,500);
	}
*/
};


